import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QFile, QDir

class MyTextEdit(QTextEdit):
    def __init__(self,parent):
        super(MyTextEdit,self).__init__()

    def dropEvent(self, e):
        drag_content = e.mimeData().text()[8:]
        self.append(drag_content)


    def isDir(self, path):
        dir_temp = QDir(path)
        return dir_temp.exists(), dir_temp

    def isFile(self, path):
        file_temp = QFile(path)
        return file_temp.exists(), file_temp

    def hand_file(self, file_path):
        with open(src_file, 'r', encoding='utf-8') as f_read, open(dst_file, 'w', encoding='utf-8') as f_write:
            for line_item in f_read:
                f_write.write(line_item)

    def hand_dir(self,file_path):
        isdir, dir = self.isDir(file_path)
        for item in dir:
            tmp_isdir, tmp_dir = self.isDir(dir)
            if tmp_isdir:
                self.hand_dir(tmp_dir)
            else:
                self.hand_file(tmp_dir)

class MainForm(QWidget):
    def __init__(self, name = 'MainForm'):
        super(MainForm,self).__init__()
        self.setWindowTitle(name)
        self.cwd = os.getcwd() # 获取当前程序文件位置
        self.resize(300,400)   # 设置窗体大小

        self.initUI()

    def initUI(self):
        # btn 1
        self.btn_start_convert = QPushButton(self)  
        self.btn_start_convert.setObjectName("btn_start_convert")
        self.btn_start_convert.setText("START!")
        self.btn_start_convert.setMinimumHeight(30)

        # textEdit
        self.textEdit = MyTextEdit(self)

        # 设置布局
        layout = QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self.btn_start_convert)
        layout.addWidget(self.textEdit)



        # 设置信号
        # self.btn_start_convert.clicked.connect(self.slot_btn_chooseFile)


    def slot_get_all(self,e):
        print(e.mimeData().text())

    def slot_btn_start_convert(self):
        dir_choose = QFileDialog.getExistingDirectory(self,  
                                    "选取文件夹",  
                                    self.cwd) # 起始路径

        if dir_choose == "":
            print("\n取消文件夹选择")
            return

        self.textEdit.setText(dir_choose)
        print(dir_choose)

    def slot_btn_chooseFile(self):
        file_choose = QFileDialog.getOpenFileNames(
            self,
            "选取文件",
            self.cwd,
            #filter='Header File(*.h *.hpp);;C/Cpp File(*.c *.cpp *.cxx)'
            filter = 'Code File(*.h *.hpp *.c *.cpp *.cxx)'

        )
        if file_choose == "":
            print("\n取消文件选择")

        for file in file_choose[0]:
            print(file)
            self.textEdit.append(file)


if __name__=="__main__":
    app = QApplication(sys.argv)
    mainForm = MainForm('测试QFileDialog')
    mainForm.show()
    sys.exit(app.exec_())
